package cn.jit.ssm.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: Cecurio
 * @create: 2017-12-17 20:38
 **/
public class ResponseUtil {
    /**
     * 使用HttpServletResponse以指定的编码返回JSON
     * @param jsonString json字符串
     * @param response HttpServletResponse实例
     * @param charset 字符的编码
     * @throws IOException
     */
    public static void json(String jsonString, HttpServletResponse response, String charset) throws IOException {
        response.setContentType("application/json;charset=" + charset);
        PrintWriter out = response.getWriter();
        out.append(jsonString);
        out.flush();
        out.close();
    }

    /**
     * 使用HttpServletResponse以UTF-8编码返回JSON
     * @param jsonString json字符串
     * @param response HttpServletResponse实例
     * @throws IOException
     */
    public static void json(String jsonString, HttpServletResponse response) throws IOException {
        json(jsonString, response, "UTF-8");
    }
}
